<?php

namespace app\im\service;

use app\im\model\UserChatroomModel;

class ConversationService
{
    protected $db;

    public function __construct()
    {
        $this->db = new UserChatroomModel();
    }

    public function getListByUserId($userId)
    {
        $rooms = $this->db->where('user_id', $userId)->column('room_id');
        // return $rooms;
        $list = $this->db->alias('ur')
            ->join('users u', 'u.id = ur.user_id')
            ->join('chatrooms room', 'room.id = ur.room_id')
            ->where('ur.room_id', 'in', $rooms)
            ->where('ur.user_id', '<>', $userId)
            ->fieldRaw('ur.user_id, u.nickname,u.avatar,room.title,room.type,ur.room_id')
            ->select();
        return $list;
    }
}
